package com.freesky.biz.icbc.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.freesky.biz.icbc.domain.TIcbcEnroll;
import com.freesky.biz.icbc.domain.TIcbcWarmWinter;
import com.freesky.biz.icbc.service.IIcbcEnrollService;
import com.freesky.biz.icbc.service.IIcbcWarmWinterService;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.file.FileUtils;
import com.freesky.common.utils.file.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
 * 活动报名
 */
@RestController
@RequestMapping("/icbc/enroll")
public class IcbcEnrollController extends BaseController {

    @Autowired
    private IIcbcEnrollService icbcEnrollService;

    @Autowired
    private OssUtils ossUtils;
    /**
     * 活动报名列表
     */
    @PreAuthorize("@ss.hasPermi('icbc:enroll:list')")
    @GetMapping("/list")
    public TableDataInfo list(TIcbcEnroll icbcEnroll) {
        startPage();
        List<TIcbcEnroll> list = icbcEnrollService.findList(icbcEnroll);
        return getDataTable(list);
    }

    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('icbc:enroll:export')")
    @GetMapping("/export")
    public AjaxResult export(TIcbcEnroll icbcEnroll,
                             HttpServletResponse response) {
//        if("1002".equalsIgnoreCase(icbcEnroll.getType())) {
//            List<TIcbcEnroll> list = icbcEnrollService.findList(icbcEnroll);
//            String zipName = "高考报名"+ DateUtil.format(new Date(),"yyyyMMdd")+".zip";
//
//            try {
//                ZipOutputStream zipOutputStream = null;
//                zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream("/Users/zhengxiansheng/Documents/trash/enroll/" + zipName)));
//                //设置压缩方法
//                zipOutputStream.setMethod(ZipOutputStream.DEFLATED);
//                for (TIcbcEnroll icbcEnroll1 : list) {
//                    String key = icbcEnroll1.getPics().replaceAll("https://ryd-files.oss-cn-shenzhen.aliyuncs.com/", "");
//                    String suffixName = key.substring(key.lastIndexOf("."));
//                    String fileName = icbcEnroll1.getName() + "_" + icbcEnroll1.getIdno() + "_" + icbcEnroll1.getMobile() + suffixName;
//                    String filePath = "/Users/zhengxiansheng/Documents/trash/enroll/" + fileName;
//                    ossUtils.getFileFromOss(key, filePath);
//                    FileUtils.toZip(zipOutputStream,fileName,filePath);
//                }
//                zipOutputStream.close();
//            } catch (Exception ioe){
//                ioe.printStackTrace();
//            }
//        }
        return icbcEnrollService.exportFile(icbcEnroll);
    }


    @Log(title = "活动报名", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('icbc:enroll:export')")
    @GetMapping("/exportShow")
    public AjaxResult exportShow(TIcbcEnroll icbcEnroll,
                             HttpServletResponse response) {
        icbcEnroll.setType("1004");
        return icbcEnrollService.exportShowFile(icbcEnroll);
    }

}
